package mwt.xml.xdbforms.dblayer.metadata.impl;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.RowIdLifetime;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import mwt.xml.xdbforms.dblayer.metadata.CatalogMetaData;

/**
 * Progetto Master Web Technology
 *
 * Definisce l'implementazione della classe astratta
 * che estende l'interfaccia DatabaseMetaData e DBMSDependedMetaData.
 * Questa soluzione e' richiesta per fornire dei metadati
 * recuperabili solo tramite interrogazioni customizzate
 * per un particolare database. Se il database di back-end è differente
 * da MySQL si dovranno fornire delle implementazioni differenti
 * per i metodi getEnumValues e getSetValues.
 *
 * @see CatalogMetaData
 * @see DBMSDependedMetaData
 * 
 *
 *
 * @author Gianfranco Murador, Cristian Castiglia, Matteo Ferri
 */
public class MySQLCatalogMetaDataDM extends CatalogMetaData {

    private Connection c = null;
    private DatabaseMetaData dmd;

    /**
     * <b>Questi parametri devono essere specificati nel costruttore
     * se la classe che estende la classe astratta CalatogMetaData
     * fa uso del DriverManager.</b>
     * @param username Nome utente per l'accesso al db
     * @param password Password di accesso al db
     * @param url Stringa di connessione al database
     */
    public MySQLCatalogMetaDataDM(String username, String password, String url) {

        try {
            c = DriverManager.getConnection(url, username, password);
            dmd = c.getMetaData();
        } catch (SQLException ex) {
        }
    }

    @Override
    public void close() {

        if (c != null) {
            try {
                c.close();
            } catch (SQLException ex) {
            }
        }

    }

    /**
     * Sono tutti metodi delegati
     *
     */

    public boolean allProceduresAreCallable() throws SQLException {
        return dmd.allProceduresAreCallable();
    }

    public boolean usesLocalFiles() throws SQLException {
        return dmd.usesLocalFiles();
    }

    public boolean usesLocalFilePerTable() throws SQLException {
        return dmd.usesLocalFilePerTable();
    }

    public boolean updatesAreDetected(int type) throws SQLException {
        return dmd.updatesAreDetected(type);
    }

    public boolean supportsUnionAll() throws SQLException {
        return dmd.supportsUnionAll();
    }

    public boolean supportsUnion() throws SQLException {
        return dmd.supportsUnion();
    }

    public boolean supportsTransactions() throws SQLException {
        return dmd.supportsTransactions();
    }

    public boolean supportsTransactionIsolationLevel(int level) throws SQLException {
        return dmd.supportsTransactionIsolationLevel(level);
    }

    public boolean supportsTableCorrelationNames() throws SQLException {
        return dmd.supportsTableCorrelationNames();
    }

    public boolean supportsSubqueriesInQuantifieds() throws SQLException {
        return dmd.supportsSubqueriesInQuantifieds();
    }

    public boolean supportsSubqueriesInIns() throws SQLException {
        return dmd.supportsSubqueriesInIns();
    }

    public boolean supportsSubqueriesInExists() throws SQLException {
        return dmd.supportsSubqueriesInExists();
    }

    public boolean supportsSubqueriesInComparisons() throws SQLException {
        return dmd.supportsSubqueriesInComparisons();
    }

    public boolean supportsStoredProcedures() throws SQLException {
        return dmd.supportsStoredProcedures();
    }

    public boolean supportsStoredFunctionsUsingCallSyntax() throws SQLException {
        return dmd.supportsStoredFunctionsUsingCallSyntax();
    }

    public boolean supportsStatementPooling() throws SQLException {
        return dmd.supportsStatementPooling();
    }

    public boolean supportsSelectForUpdate() throws SQLException {
        return dmd.supportsSelectForUpdate();
    }

    public boolean supportsSchemasInTableDefinitions() throws SQLException {
        return dmd.supportsSchemasInTableDefinitions();
    }

    public boolean supportsSchemasInProcedureCalls() throws SQLException {
        return dmd.supportsSchemasInProcedureCalls();
    }

    public boolean supportsSchemasInPrivilegeDefinitions() throws SQLException {
        return dmd.supportsSchemasInPrivilegeDefinitions();
    }

    public boolean supportsSchemasInIndexDefinitions() throws SQLException {
        return dmd.supportsSchemasInIndexDefinitions();
    }

    public boolean supportsSchemasInDataManipulation() throws SQLException {
        return dmd.supportsSchemasInDataManipulation();
    }

    public boolean supportsSavepoints() throws SQLException {
        return dmd.supportsSavepoints();
    }

    public boolean supportsResultSetType(int type) throws SQLException {
        return dmd.supportsResultSetType(type);
    }

    public boolean supportsResultSetHoldability(int holdability) throws SQLException {
        return dmd.supportsResultSetHoldability(holdability);
    }

    public boolean supportsResultSetConcurrency(int type, int concurrency) throws SQLException {
        return dmd.supportsResultSetConcurrency(type, concurrency);
    }

    public boolean supportsPositionedUpdate() throws SQLException {
        return dmd.supportsPositionedUpdate();
    }

    public boolean supportsPositionedDelete() throws SQLException {
        return dmd.supportsPositionedDelete();
    }

    public boolean supportsOuterJoins() throws SQLException {
        return dmd.supportsOuterJoins();
    }

    public boolean supportsOrderByUnrelated() throws SQLException {
        return dmd.supportsOrderByUnrelated();
    }

    public boolean supportsOpenStatementsAcrossRollback() throws SQLException {
        return dmd.supportsOpenStatementsAcrossRollback();
    }

    public boolean supportsOpenStatementsAcrossCommit() throws SQLException {
        return dmd.supportsOpenStatementsAcrossCommit();
    }

    public boolean supportsOpenCursorsAcrossRollback() throws SQLException {
        return dmd.supportsOpenCursorsAcrossRollback();
    }

    public boolean supportsOpenCursorsAcrossCommit() throws SQLException {
        return dmd.supportsOpenCursorsAcrossCommit();
    }

    public boolean supportsNonNullableColumns() throws SQLException {
        return dmd.supportsNonNullableColumns();
    }

    public boolean supportsNamedParameters() throws SQLException {
        return dmd.supportsNamedParameters();
    }

    public boolean supportsMultipleTransactions() throws SQLException {
        return dmd.supportsMultipleTransactions();
    }

    public boolean supportsMultipleResultSets() throws SQLException {
        return dmd.supportsMultipleResultSets();
    }

    public boolean supportsMultipleOpenResults() throws SQLException {
        return dmd.supportsMultipleOpenResults();
    }

    public boolean supportsMixedCaseQuotedIdentifiers() throws SQLException {
        return dmd.supportsMixedCaseQuotedIdentifiers();
    }

    public boolean supportsMixedCaseIdentifiers() throws SQLException {
        return dmd.supportsMixedCaseIdentifiers();
    }

    public boolean supportsMinimumSQLGrammar() throws SQLException {
        return dmd.supportsMinimumSQLGrammar();
    }

    public boolean supportsLimitedOuterJoins() throws SQLException {
        return dmd.supportsLimitedOuterJoins();
    }

    public boolean supportsLikeEscapeClause() throws SQLException {
        return dmd.supportsLikeEscapeClause();
    }

    public boolean supportsIntegrityEnhancementFacility() throws SQLException {
        return dmd.supportsIntegrityEnhancementFacility();
    }

    public boolean supportsGroupByUnrelated() throws SQLException {
        return dmd.supportsGroupByUnrelated();
    }

    public boolean supportsGroupByBeyondSelect() throws SQLException {
        return dmd.supportsGroupByBeyondSelect();
    }

    public boolean supportsGroupBy() throws SQLException {
        return dmd.supportsGroupBy();
    }

    public boolean supportsGetGeneratedKeys() throws SQLException {
        return dmd.supportsGetGeneratedKeys();
    }

    public boolean supportsFullOuterJoins() throws SQLException {
        return dmd.supportsFullOuterJoins();
    }

    public boolean supportsExtendedSQLGrammar() throws SQLException {
        return dmd.supportsExtendedSQLGrammar();
    }

    public boolean supportsExpressionsInOrderBy() throws SQLException {
        return dmd.supportsExpressionsInOrderBy();
    }

    public boolean supportsDifferentTableCorrelationNames() throws SQLException {
        return dmd.supportsDifferentTableCorrelationNames();
    }

    public boolean supportsDataManipulationTransactionsOnly() throws SQLException {
        return dmd.supportsDataManipulationTransactionsOnly();
    }

    public boolean supportsDataDefinitionAndDataManipulationTransactions() throws SQLException {
        return dmd.supportsDataDefinitionAndDataManipulationTransactions();
    }

    public boolean supportsCorrelatedSubqueries() throws SQLException {
        return dmd.supportsCorrelatedSubqueries();
    }

    public boolean supportsCoreSQLGrammar() throws SQLException {
        return dmd.supportsCoreSQLGrammar();
    }

    public boolean supportsConvert(int fromType, int toType) throws SQLException {
        return dmd.supportsConvert(fromType, toType);
    }

    public boolean supportsConvert() throws SQLException {
        return dmd.supportsConvert();
    }

    public boolean supportsColumnAliasing() throws SQLException {
        return dmd.supportsColumnAliasing();
    }

    public boolean supportsCatalogsInTableDefinitions() throws SQLException {
        return dmd.supportsCatalogsInTableDefinitions();
    }

    public boolean supportsCatalogsInProcedureCalls() throws SQLException {
        return dmd.supportsCatalogsInProcedureCalls();
    }

    public boolean supportsCatalogsInPrivilegeDefinitions() throws SQLException {
        return dmd.supportsCatalogsInPrivilegeDefinitions();
    }

    public boolean supportsCatalogsInIndexDefinitions() throws SQLException {
        return dmd.supportsCatalogsInIndexDefinitions();
    }

    public boolean supportsCatalogsInDataManipulation() throws SQLException {
        return dmd.supportsCatalogsInDataManipulation();
    }

    public boolean supportsBatchUpdates() throws SQLException {
        return dmd.supportsBatchUpdates();
    }

    public boolean supportsAlterTableWithDropColumn() throws SQLException {
        return dmd.supportsAlterTableWithDropColumn();
    }

    public boolean supportsAlterTableWithAddColumn() throws SQLException {
        return dmd.supportsAlterTableWithAddColumn();
    }

    public boolean supportsANSI92IntermediateSQL() throws SQLException {
        return dmd.supportsANSI92IntermediateSQL();
    }

    public boolean supportsANSI92FullSQL() throws SQLException {
        return dmd.supportsANSI92FullSQL();
    }

    public boolean supportsANSI92EntryLevelSQL() throws SQLException {
        return dmd.supportsANSI92EntryLevelSQL();
    }

    public boolean storesUpperCaseQuotedIdentifiers() throws SQLException {
        return dmd.storesUpperCaseQuotedIdentifiers();
    }

    public boolean storesUpperCaseIdentifiers() throws SQLException {
        return dmd.storesUpperCaseIdentifiers();
    }

    public boolean storesMixedCaseQuotedIdentifiers() throws SQLException {
        return dmd.storesMixedCaseQuotedIdentifiers();
    }

    public boolean storesMixedCaseIdentifiers() throws SQLException {
        return dmd.storesMixedCaseIdentifiers();
    }

    public boolean storesLowerCaseQuotedIdentifiers() throws SQLException {
        return dmd.storesLowerCaseQuotedIdentifiers();
    }

    public boolean storesLowerCaseIdentifiers() throws SQLException {
        return dmd.storesLowerCaseIdentifiers();
    }

    public boolean ownUpdatesAreVisible(int type) throws SQLException {
        return dmd.ownUpdatesAreVisible(type);
    }

    public boolean ownInsertsAreVisible(int type) throws SQLException {
        return dmd.ownInsertsAreVisible(type);
    }

    public boolean ownDeletesAreVisible(int type) throws SQLException {
        return dmd.ownDeletesAreVisible(type);
    }

    public boolean othersUpdatesAreVisible(int type) throws SQLException {
        return dmd.othersUpdatesAreVisible(type);
    }

    public boolean othersInsertsAreVisible(int type) throws SQLException {
        return dmd.othersInsertsAreVisible(type);
    }

    public boolean othersDeletesAreVisible(int type) throws SQLException {
        return dmd.othersDeletesAreVisible(type);
    }

    public boolean nullsAreSortedLow() throws SQLException {
        return dmd.nullsAreSortedLow();
    }

    public boolean nullsAreSortedHigh() throws SQLException {
        return dmd.nullsAreSortedHigh();
    }

    public boolean nullsAreSortedAtStart() throws SQLException {
        return dmd.nullsAreSortedAtStart();
    }

    public boolean nullsAreSortedAtEnd() throws SQLException {
        return dmd.nullsAreSortedAtEnd();
    }

    public boolean nullPlusNonNullIsNull() throws SQLException {
        return dmd.nullPlusNonNullIsNull();
    }

    public boolean locatorsUpdateCopy() throws SQLException {
        return dmd.locatorsUpdateCopy();
    }

    public boolean isReadOnly() throws SQLException {
        return dmd.isReadOnly();
    }

    public boolean isCatalogAtStart() throws SQLException {
        return dmd.isCatalogAtStart();
    }

    public boolean insertsAreDetected(int type) throws SQLException {
        return dmd.insertsAreDetected(type);
    }

    public ResultSet getVersionColumns(String catalog, String schema, String table) throws SQLException {
        return dmd.getVersionColumns(catalog, schema, table);
    }

    public String getUserName() throws SQLException {
        return dmd.getUserName();
    }

    public String getURL() throws SQLException {
        return dmd.getURL();
    }

    public ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types) throws SQLException {
        return dmd.getUDTs(catalog, schemaPattern, typeNamePattern, types);
    }

    public ResultSet getTypeInfo() throws SQLException {
        return dmd.getTypeInfo();
    }

    public String getTimeDateFunctions() throws SQLException {
        return dmd.getTimeDateFunctions();
    }

    public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) throws SQLException {
        return dmd.getTables(catalog, schemaPattern, tableNamePattern, types);
    }

    public ResultSet getTableTypes() throws SQLException {
        return dmd.getTableTypes();
    }

    public ResultSet getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern) throws SQLException {
        return dmd.getTablePrivileges(catalog, schemaPattern, tableNamePattern);
    }

    public String getSystemFunctions() throws SQLException {
        return dmd.getSystemFunctions();
    }

    public ResultSet getSuperTypes(String catalog, String schemaPattern, String typeNamePattern) throws SQLException {
        return dmd.getSuperTypes(catalog, schemaPattern, typeNamePattern);
    }

    public ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws SQLException {
        return dmd.getSuperTables(catalog, schemaPattern, tableNamePattern);
    }

    public String getStringFunctions() throws SQLException {
        return dmd.getStringFunctions();
    }

    public String getSearchStringEscape() throws SQLException {
        return dmd.getSearchStringEscape();
    }

    public ResultSet getSchemas(String catalog, String schemaPattern) throws SQLException {
        return dmd.getSchemas(catalog, schemaPattern);
    }

    public ResultSet getSchemas() throws SQLException {
        return dmd.getSchemas();
    }

    public String getSchemaTerm() throws SQLException {
        return dmd.getSchemaTerm();
    }

    public int getSQLStateType() throws SQLException {
        return dmd.getSQLStateType();
    }

    public String getSQLKeywords() throws SQLException {
        return dmd.getSQLKeywords();
    }

    public RowIdLifetime getRowIdLifetime() throws SQLException {
        return dmd.getRowIdLifetime();
    }

    public int getResultSetHoldability() throws SQLException {
        return dmd.getResultSetHoldability();
    }

    public ResultSet getProcedures(String catalog, String schemaPattern, String procedureNamePattern) throws SQLException {
        return dmd.getProcedures(catalog, schemaPattern, procedureNamePattern);
    }

    public String getProcedureTerm() throws SQLException {
        return dmd.getProcedureTerm();
    }

    public ResultSet getProcedureColumns(String catalog, String schemaPattern, String procedureNamePattern, String columnNamePattern) throws SQLException {
        return dmd.getProcedureColumns(catalog, schemaPattern, procedureNamePattern, columnNamePattern);
    }

    public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException {
        return dmd.getPrimaryKeys(catalog, schema, table);
    }

    public String getNumericFunctions() throws SQLException {
        return dmd.getNumericFunctions();
    }

    public int getMaxUserNameLength() throws SQLException {
        return dmd.getMaxUserNameLength();
    }

    public int getMaxTablesInSelect() throws SQLException {
        return dmd.getMaxTablesInSelect();
    }

    public int getMaxTableNameLength() throws SQLException {
        return dmd.getMaxTableNameLength();
    }

    public int getMaxStatements() throws SQLException {
        return dmd.getMaxStatements();
    }

    public int getMaxStatementLength() throws SQLException {
        return dmd.getMaxStatementLength();
    }

    public int getMaxSchemaNameLength() throws SQLException {
        return dmd.getMaxSchemaNameLength();
    }

    public int getMaxRowSize() throws SQLException {
        return dmd.getMaxRowSize();
    }

    public int getMaxProcedureNameLength() throws SQLException {
        return dmd.getMaxProcedureNameLength();
    }

    public int getMaxIndexLength() throws SQLException {
        return dmd.getMaxIndexLength();
    }

    public int getMaxCursorNameLength() throws SQLException {
        return dmd.getMaxCursorNameLength();
    }

    public int getMaxConnections() throws SQLException {
        return dmd.getMaxConnections();
    }

    public int getMaxColumnsInTable() throws SQLException {
        return dmd.getMaxColumnsInTable();
    }

    public int getMaxColumnsInSelect() throws SQLException {
        return dmd.getMaxColumnsInSelect();
    }

    public int getMaxColumnsInOrderBy() throws SQLException {
        return dmd.getMaxColumnsInOrderBy();
    }

    public int getMaxColumnsInIndex() throws SQLException {
        return dmd.getMaxColumnsInIndex();
    }

    public int getMaxColumnsInGroupBy() throws SQLException {
        return dmd.getMaxColumnsInGroupBy();
    }

    public int getMaxColumnNameLength() throws SQLException {
        return dmd.getMaxColumnNameLength();
    }

    public int getMaxCharLiteralLength() throws SQLException {
        return dmd.getMaxCharLiteralLength();
    }

    public int getMaxCatalogNameLength() throws SQLException {
        return dmd.getMaxCatalogNameLength();
    }

    public int getMaxBinaryLiteralLength() throws SQLException {
        return dmd.getMaxBinaryLiteralLength();
    }

    public int getJDBCMinorVersion() throws SQLException {
        return dmd.getJDBCMinorVersion();
    }

    public int getJDBCMajorVersion() throws SQLException {
        return dmd.getJDBCMajorVersion();
    }

    public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) throws SQLException {
        return dmd.getIndexInfo(catalog, schema, table, unique, approximate);
    }

    public ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException {
        return dmd.getImportedKeys(catalog, schema, table);
    }

    public String getIdentifierQuoteString() throws SQLException {
        return dmd.getIdentifierQuoteString();
    }

    public ResultSet getFunctions(String catalog, String schemaPattern, String functionNamePattern) throws SQLException {
        return dmd.getFunctions(catalog, schemaPattern, functionNamePattern);
    }

    public ResultSet getFunctionColumns(String catalog, String schemaPattern, String functionNamePattern, String columnNamePattern) throws SQLException {
        return dmd.getFunctionColumns(catalog, schemaPattern, functionNamePattern, columnNamePattern);
    }

    public String getExtraNameCharacters() throws SQLException {
        return dmd.getExtraNameCharacters();
    }

    public ResultSet getExportedKeys(String catalog, String schema, String table) throws SQLException {
        return dmd.getExportedKeys(catalog, schema, table);
    }

    public String getDriverVersion() throws SQLException {
        return dmd.getDriverVersion();
    }

    public String getDriverName() throws SQLException {
        return dmd.getDriverName();
    }

    public int getDriverMinorVersion() {
        return dmd.getDriverMinorVersion();
    }

    public int getDriverMajorVersion() {
        return dmd.getDriverMajorVersion();
    }

    public int getDefaultTransactionIsolation() throws SQLException {
        return dmd.getDefaultTransactionIsolation();
    }

    public String getDatabaseProductVersion() throws SQLException {
        return dmd.getDatabaseProductVersion();
    }

    public String getDatabaseProductName() throws SQLException {
        return dmd.getDatabaseProductName();
    }

    public int getDatabaseMinorVersion() throws SQLException {
        return dmd.getDatabaseMinorVersion();
    }

    public int getDatabaseMajorVersion() throws SQLException {
        return dmd.getDatabaseMajorVersion();
    }

    public ResultSet getCrossReference(String parentCatalog, String parentSchema, String parentTable, String foreignCatalog, String foreignSchema, String foreignTable) throws SQLException {
        return dmd.getCrossReference(parentCatalog, parentSchema, parentTable, foreignCatalog, foreignSchema, foreignTable);
    }

    public Connection getConnection() throws SQLException {
        return dmd.getConnection();
    }

    public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException {
        return dmd.getColumns(catalog, schemaPattern, tableNamePattern, columnNamePattern);
    }

    public ResultSet getColumnPrivileges(String catalog, String schema, String table, String columnNamePattern) throws SQLException {
        return dmd.getColumnPrivileges(catalog, schema, table, columnNamePattern);
    }

    public ResultSet getClientInfoProperties() throws SQLException {
        return dmd.getClientInfoProperties();
    }

    public ResultSet getCatalogs() throws SQLException {
        return dmd.getCatalogs();
    }

    public String getCatalogTerm() throws SQLException {
        return dmd.getCatalogTerm();
    }

    public String getCatalogSeparator() throws SQLException {
        return dmd.getCatalogSeparator();
    }

    public ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable) throws SQLException {
        return dmd.getBestRowIdentifier(catalog, schema, table, scope, nullable);
    }

    public ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern, String attributeNamePattern) throws SQLException {
        return dmd.getAttributes(catalog, schemaPattern, typeNamePattern, attributeNamePattern);
    }

    public boolean doesMaxRowSizeIncludeBlobs() throws SQLException {
        return dmd.doesMaxRowSizeIncludeBlobs();
    }

    public boolean deletesAreDetected(int type) throws SQLException {
        return dmd.deletesAreDetected(type);
    }

    public boolean dataDefinitionIgnoredInTransactions() throws SQLException {
        return dmd.dataDefinitionIgnoredInTransactions();
    }

    public boolean dataDefinitionCausesTransactionCommit() throws SQLException {
        return dmd.dataDefinitionCausesTransactionCommit();
    }

    public boolean autoCommitFailureClosesAllResultSets() throws SQLException {
        return dmd.autoCommitFailureClosesAllResultSets();
    }

    public boolean allTablesAreSelectable() throws SQLException {
        return dmd.allTablesAreSelectable();
    }

    public <T> T unwrap(Class<T> iface) throws SQLException {
        return dmd.unwrap(iface);
    }

    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return dmd.isWrapperFor(iface);
    }

    /**
     * Ritorna i valori ammissibili per un campo Enum.
     * Questa funzionalità è dipendente dal database.
     * @param table nome della tabella
     * @param colname nome della colonna
     * @return array di stringhe contenete tutti i valori ammissibili
     * @throws java.lang.RuntimeException
     */
    public String[] getEnumValues(String table, String colname) throws RuntimeException {

        String sql = "SHOW COLUMNS FROM " + table + " WHERE field=?";
        String fieldType;

        PreparedStatement ps;
        ResultSet rs = null;
        int position = 0;
        List<String> values = new ArrayList<String>();
        String value = null;

        try {
            ps = c.prepareStatement(sql);
            ps.setString(1, colname);
            rs = ps.executeQuery();

            rs.next();
            fieldType = rs.getString("Type");

            if (fieldType == null) {
                throw new RuntimeException("Column " + colname + " not exists in table " + table);
            }

            // Parso la stringa per capire se mi trovo di fronte ad un set
            if (fieldType.startsWith("enum")) {

                // Prendo i valori ammissibili per set

                while ((position = fieldType.indexOf("'", position)) > 0) {
                    int secondPosition = fieldType.indexOf("'", position + 1);
                    value = fieldType.substring(position + 1, secondPosition);
                    values.add(value);
                    position = secondPosition + 1;
                }

            } else {

                throw new RuntimeException("Column " + colname + " is not a ENUM type");

            }

        } catch (SQLException ex) {
            Logger.getLogger(MySQLCatalogMetaDataDM.class.getName()).log(Level.SEVERE, null, ex);
        }

        return values.toArray(new String[0]);


    }

    public String[] getSetValues(String table, String colname) throws RuntimeException {

        String sql = "SHOW COLUMNS FROM " + table + " WHERE field=?";
        PreparedStatement ps;
        int position = 0;
        List<String> values = new ArrayList<String>();
        String value = null;
        String fieldType = null;

        ResultSet rs = null;
        try {
            ps = c.prepareStatement(sql);
            ps.setString(1, colname);
            rs = ps.executeQuery();

            rs.next();

            fieldType = rs.getString("Type");


            if (fieldType == null) {
                throw new RuntimeException("Column " + colname + " not exists in table " + table);
            }

            // Parso la stringa per capire se mi trovo di fronte ad un set
            if (fieldType.startsWith("set")) {

                // Prendo i valori ammissibili per set

                while ((position = fieldType.indexOf("'", position)) > 0) {
                    int secondPosition = fieldType.indexOf("'", position + 1);
                    value = fieldType.substring(position + 1, secondPosition);
                    values.add(value);
                    position = secondPosition + 1;
                }

            } else {

                throw new RuntimeException("Column " + colname + " is not a SET type");

            }

        } catch (SQLException ex) {
            Logger.getLogger(MySQLCatalogMetaDataDM.class.getName()).log(Level.SEVERE, null, ex);
        }

        

        return values.toArray(new String[0]);



    }
}
